package com.xkdx.guangguang.fragment.brand;

import java.util.HashMap;
import java.util.List;

import android.R.integer;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.xkdx.caipiao.HomePageActivity;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.fragment.brand.search.SearchBrandFragment;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.shareclass.BrandDetail;
import com.xkdx.guangguang.sql.BrandDataHelper;
import com.xkdx.guangguang.sql.BrandSQLiteHelper;

public class BrandFragment extends BaseFragment implements OnTouchListener,
		OnGestureListener {
	View view;
	ImageLoader imageLoader = ImageLoader.getInstance();
	ImageView shop, food, entertement, lift;
	Button search, showMap, atoz, refresh;
	ImageView back;
	RelativeLayout shop_ll, food_ll, entertaiment_ll, life_ll;
	TextView number_shop, number_entertainment, number_food, number_life;
	DisplayImageOptions options;
	ViewFlipper brand_adverse_image;
	ImageView point1, point2, point3;
	Drawable[] drawables;
	public static List<BrandDetail> shopBrandList;
	public static List<BrandDetail> entertaimentBrandList;
	public static List<BrandDetail> lifeBrandList;
	public static List<BrandDetail> foodBrandList;
	int[] brandId = {  901, 1234 };
	GestureDetector detector;
	int currentNum = 0;
	BrandDataHelper helper;
	BrandTask task;
	private final static int BRANDMAINSHOWNUM = 12;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.empty_photo)
				.showImageForEmptyUri(R.drawable.empty_photo)
				.showImageOnFail(R.drawable.empty_photo)
				.resetViewBeforeLoading().cacheOnDisc()
				.imageScaleType(ImageScaleType.EXACTLY)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.brand, container, false);
		findView();
		setOnClick();
		showLoading();
		return view;
	}

	@Override
	public void findView() {
		drawables = new Drawable[] {
				getResources().getDrawable(R.drawable.kadiya),
				getResources().getDrawable(R.drawable.xiangnaier) };
		search = (Button) view.findViewById(R.id.search_stroll_brand);
//		atoz = (Button) view.findViewById(R.id.AtoZ);
//		refresh = (Button) view.findViewById(R.id.brand_refresh);
		back = (ImageView) view.findViewById(R.id.brand_back);
		shop_ll = (RelativeLayout) view
				.findViewById(R.id.brand_classification_shop_ll);
		entertaiment_ll = (RelativeLayout) view
				.findViewById(R.id.brand_classification_entertaiment_ll);
		life_ll = (RelativeLayout) view
				.findViewById(R.id.brand_classification_life_ll);
		food_ll = (RelativeLayout) view
				.findViewById(R.id.brand_classification_food_ll);

		detector = new GestureDetector(this);
		brand_adverse_image = (ViewFlipper) view
				.findViewById(R.id.brand_adverse_image);
//		point1 = (ImageView) view.findViewById(R.id.brand_point1);
		point2 = (ImageView) view.findViewById(R.id.brand_point2);
		point3 = (ImageView) view.findViewById(R.id.brand_point3);

		number_shop = (TextView) view.findViewById(R.id.brand_shopclass_number);
		number_entertainment = (TextView) view
				.findViewById(R.id.brand_entertainmentclass_number);
		number_food = (TextView) view.findViewById(R.id.brand_foodclass_number);
		number_life = (TextView) view.findViewById(R.id.brand_lifeclass_number);
		if (brand_adverse_image != null) {
			brand_adverse_image.removeAllViews();
		}
		for (int i = 0; i < drawables.length; i++) {
			ImageView imageview = new ImageView(getActivity());
			imageview.setImageDrawable(drawables[i]);
			imageview.setScaleType(ImageView.ScaleType.FIT_XY);
			brand_adverse_image.addView(imageview);
		}

		brand_adverse_image.setOnTouchListener(this);
		brand_adverse_image.setLongClickable(true);

	}

	@Override
	public void onPause() {
		currentNum = 0;
		super.onPause();
	}

	@Override
	public void setOnClick() {
		shop_ll.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				MoreBrandFragment moreBrandFragment = new MoreBrandFragment();
				Bundle b = new Bundle();
				b.putString("tag", "购物类");
				moreBrandFragment.setArguments(b);
				getFragmentManager().beginTransaction()
						.replace(R.id.fragment, moreBrandFragment)
						.addToBackStack(null).commit();

			}
		});
		entertaiment_ll.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				MoreBrandFragment moreBrandFragment = new MoreBrandFragment();
				Bundle b = new Bundle();
				b.putString("tag", "娱乐类");
				moreBrandFragment.setArguments(b);
				getFragmentManager().beginTransaction()
						.replace(R.id.fragment, moreBrandFragment)
						.addToBackStack(null).commit();

			}
		});

		life_ll.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				MoreBrandFragment moreBrandFragment = new MoreBrandFragment();
				Bundle b = new Bundle();
				b.putString("tag", "生活类");
				moreBrandFragment.setArguments(b);
				getFragmentManager().beginTransaction()
						.replace(R.id.fragment, moreBrandFragment)
						.addToBackStack(null).commit();

			}
		});
		food_ll.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				MoreBrandFragment moreBrandFragment = new MoreBrandFragment();
				Bundle b = new Bundle();
				b.putString("tag", "美食类");
				moreBrandFragment.setArguments(b);
				getFragmentManager().beginTransaction()
						.replace(R.id.fragment, moreBrandFragment)
						.addToBackStack(null).commit();

			}
		});
		search.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				SearchBrandFragment searchBrandFragment = new SearchBrandFragment();
				getFragmentManager().beginTransaction()
						.replace(R.id.fragment, searchBrandFragment)
						.addToBackStack(null).commit();

			}
		});
		
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				getActivity().finish();
				
			}
		});

//		atoz.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				AtoZFragment atoZFragment = new AtoZFragment();
//				getFragmentManager().beginTransaction()
//						.replace(R.id.fragment, atoZFragment)
//						.addToBackStack(null).commit();
//
//			}
//		});


	}

	@Override
	public void showLoading() {

		if (entertaimentBrandList != null && shopBrandList != null
				&& foodBrandList != null && lifeBrandList != null
				&& entertaimentBrandList.size() >= BRANDMAINSHOWNUM
				&& shopBrandList.size() >= BRANDMAINSHOWNUM && foodBrandList.size() >= BRANDMAINSHOWNUM
				&& lifeBrandList.size() >= BRANDMAINSHOWNUM) {
			setBrandValuse();
		} else {
			task = new BrandTask();
			task.execute();
			dataLoadDialog.setOnCancelListener(new OnCancelListener() {

				@Override
				public void onCancel(DialogInterface dialog) {
					getActivity().finish();
				}
			});
			dataLoadDialog.show();
		}

	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {

	}

	@Override
	public void showClick(HashMap<String, AbsModule> map) {

	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {

	}

	@Override
	public boolean onDown(MotionEvent e) {

		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		/*FragmentTransaction ft = getFragmentManager().beginTransaction();
		BrandInfoFragment brandInfoFragment = new BrandInfoFragment();
		Bundle b2 = new Bundle();
		b2.putString("brandID", String.valueOf(brandId[currentNum]));
		brandInfoFragment.setArguments(b2);
		ft.replace(R.id.fragment, brandInfoFragment);
		ft.addToBackStack(null);
		ft.commit();*/
		return false;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {

		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {

	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		if (e1.getX() - e2.getX() > 120) {
			brand_adverse_image.setInAnimation(AnimationUtils.loadAnimation(
					getActivity(), R.anim.left_in));
			brand_adverse_image.setOutAnimation(AnimationUtils.loadAnimation(
					getActivity(), R.anim.left_out));
			currentNum++;
			if (currentNum > 1) {
				currentNum = 0;
			}
			setPoint(currentNum);
			brand_adverse_image.showNext();
			return true;
		} else if (e1.getX() - e2.getX() < -120) {
			brand_adverse_image.setInAnimation(AnimationUtils.loadAnimation(
					getActivity(), R.anim.right_in));
			brand_adverse_image.setOutAnimation(AnimationUtils.loadAnimation(
					getActivity(), R.anim.right_out));

			currentNum--;
			if (currentNum < 0) {
				currentNum = 1;
			}
			setPoint(currentNum);
			brand_adverse_image.showPrevious();
			return true;
		}
		return false;
	}

	private void setPoint(int index) {
		switch (index) {
		case 0:
//			point1.setEnabled(false);
			point2.setEnabled(true);
			point3.setEnabled(true);
			break;
		case 1:
//			point1.setEnabled(true);
			point2.setEnabled(false);
			point3.setEnabled(true);
			break;
		case 2:
//			point1.setEnabled(true);
			point2.setEnabled(true);
			point3.setEnabled(false);
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {

		return detector.onTouchEvent(event);
	}

	class BrandTask extends AsyncTask<String, integer, String> {

		@Override
		protected String doInBackground(String... params) {
			try {
				helper = new BrandDataHelper(getActivity(),
						BrandSQLiteHelper.ATOZTABLE, false);
				if (HomePageActivity.upDateBrandlist.size() != 0) {
					helper.upDateList(HomePageActivity.upDateBrandlist);
					HomePageActivity.upDateBrandlist.clear();
				}

				shopBrandList = helper.getBrandListByBrandType("1");
				entertaimentBrandList = helper.getBrandListByBrandType("2");
				lifeBrandList = helper.getBrandListByBrandType("3");
				foodBrandList = helper.getBrandListByBrandType("4");
			} catch (Exception e) {
				return "false";
			} finally {
				helper.Close();
			}
			return "success";
		}

		@Override
		protected void onPostExecute(String result) {
			if (result.equals("success")) {
				setBrandValuse();
			}
		}
	}

	private void setBrandValuse() {
		if (getActivity() == null) {
			return;
		}

		setEntertainmentValue(entertaimentBrandList);
		setFoodValue(foodBrandList);
		setLifeValue(lifeBrandList);
		setShopValue(shopBrandList);
		number_shop.setText(shopBrandList.size() + "");
		number_entertainment.setText(entertaimentBrandList.size() + "");
		number_food.setText(foodBrandList.size() + "");
		number_life.setText(lifeBrandList.size() + "");
		dataLoadDialog.dismiss();
	}

	protected void setShopValue(List<BrandDetail> list) {
		// shop
		ImageView im1 = (ImageView) view.findViewById(R.id.classification_1)
				.findViewById(R.id.brand_image);
		TextView tv1 = (TextView) view.findViewById(R.id.classification_1)
				.findViewById(R.id.brand_item_name);
		tv1.setText(list.get(0).getBrandSimpleName());
		imageLoader.displayImage(list.get(0).getLogo(), im1, options);
		ImageView im2 = (ImageView) view.findViewById(R.id.classification_2)
				.findViewById(R.id.brand_image);
		TextView tv2 = (TextView) view.findViewById(R.id.classification_2)
				.findViewById(R.id.brand_item_name);
		tv2.setText(list.get(1).getBrandSimpleName());
		imageLoader.displayImage(list.get(1).getLogo(), im2, options);
		ImageView im3 = (ImageView) view.findViewById(R.id.classification_3)
				.findViewById(R.id.brand_image);
		TextView tv3 = (TextView) view.findViewById(R.id.classification_3)
				.findViewById(R.id.brand_item_name);
		tv3.setText(list.get(2).getBrandSimpleName());
		imageLoader.displayImage(list.get(2).getLogo(), im3, options);
		ImageView im4 = (ImageView) view.findViewById(R.id.classification_4)
				.findViewById(R.id.brand_image);
		TextView tv4 = (TextView) view.findViewById(R.id.classification_4)
				.findViewById(R.id.brand_item_name);
		tv4.setText(list.get(3).getBrandSimpleName());
		imageLoader.displayImage(list.get(3).getLogo(), im4, options);
		ImageView im5 = (ImageView) view.findViewById(R.id.classification_5)
				.findViewById(R.id.brand_image);
		TextView tv5 = (TextView) view.findViewById(R.id.classification_5)
				.findViewById(R.id.brand_item_name);
		tv5.setText(list.get(4).getBrandSimpleName());
		imageLoader.displayImage(list.get(4).getLogo(), im5, options);
		ImageView im6 = (ImageView) view.findViewById(R.id.classification_6)
				.findViewById(R.id.brand_image);
		TextView tv6 = (TextView) view.findViewById(R.id.classification_6)
				.findViewById(R.id.brand_item_name);
		tv6.setText(list.get(5).getBrandSimpleName());
		imageLoader.displayImage(list.get(5).getLogo(), im6, options);
		ImageView im7 = (ImageView) view.findViewById(R.id.classification_7)
				.findViewById(R.id.brand_image);
		TextView tv7 = (TextView) view.findViewById(R.id.classification_7)
				.findViewById(R.id.brand_item_name);
		tv7.setText(list.get(6).getBrandSimpleName());
		imageLoader.displayImage(list.get(6).getLogo(), im7, options);
		ImageView im8 = (ImageView) view.findViewById(R.id.classification_8)
				.findViewById(R.id.brand_image);
		TextView tv8 = (TextView) view.findViewById(R.id.classification_8)
				.findViewById(R.id.brand_item_name);
		tv8.setText(list.get(7).getBrandSimpleName());
		imageLoader.displayImage(list.get(7).getLogo(), im8, options);

		ImageView im9 = (ImageView) view.findViewById(R.id.classification_9)
				.findViewById(R.id.brand_image);
		TextView tv9 = (TextView) view.findViewById(R.id.classification_9)
				.findViewById(R.id.brand_item_name);
		tv9.setText(list.get(8).getBrandSimpleName());
		imageLoader.displayImage(list.get(8).getLogo(), im9, options);
		ImageView im10 = (ImageView) view.findViewById(R.id.classification_10)
				.findViewById(R.id.brand_image);
		TextView tv10 = (TextView) view.findViewById(R.id.classification_10)
				.findViewById(R.id.brand_item_name);
		tv10.setText(list.get(9).getBrandSimpleName());
		imageLoader.displayImage(list.get(9).getLogo(), im10, options);
		ImageView im11 = (ImageView) view.findViewById(R.id.classification_11)
				.findViewById(R.id.brand_image);
		TextView tv11 = (TextView) view.findViewById(R.id.classification_11)
				.findViewById(R.id.brand_item_name);
		tv11.setText(list.get(10).getBrandSimpleName());
		imageLoader.displayImage(list.get(10).getLogo(), im11, options);
		ImageView im12 = (ImageView) view.findViewById(R.id.classification_12)
				.findViewById(R.id.brand_image);
		TextView tv12 = (TextView) view.findViewById(R.id.classification_12)
				.findViewById(R.id.brand_item_name);
		tv12.setText(list.get(11).getBrandSimpleName());
		imageLoader.displayImage(list.get(11).getLogo(), im12, options);

		setImageClick(im1, list, 0);
		setImageClick(im2, list, 1);
		setImageClick(im3, list, 2);
		setImageClick(im4, list, 3);
		setImageClick(im5, list, 4);
		setImageClick(im6, list, 5);
		setImageClick(im7, list, 6);
		setImageClick(im8, list, 7);
		setImageClick(im9, list, 8);
		setImageClick(im10, list, 9);
		setImageClick(im11, list, 10);
		setImageClick(im12, list, 11);
	}

	protected void setLifeValue(List<BrandDetail> list) {
		ImageView im1 = (ImageView) view.findViewById(R.id.life_1)
				.findViewById(R.id.brand_image);
		TextView tv1 = (TextView) view.findViewById(R.id.life_1).findViewById(
				R.id.brand_item_name);
		tv1.setText(list.get(0).getBrandSimpleName());
		imageLoader.displayImage(list.get(0).getLogo(), im1, options);
		ImageView im2 = (ImageView) view.findViewById(R.id.life_2)
				.findViewById(R.id.brand_image);
		TextView tv2 = (TextView) view.findViewById(R.id.life_2).findViewById(
				R.id.brand_item_name);
		tv2.setText(list.get(1).getBrandSimpleName());
		imageLoader.displayImage(list.get(1).getLogo(), im2, options);
		ImageView im3 = (ImageView) view.findViewById(R.id.life_3)
				.findViewById(R.id.brand_image);
		TextView tv3 = (TextView) view.findViewById(R.id.life_3).findViewById(
				R.id.brand_item_name);
		tv3.setText(list.get(2).getBrandSimpleName());
		imageLoader.displayImage(list.get(2).getLogo(), im3, options);
		ImageView im4 = (ImageView) view.findViewById(R.id.life_4)
				.findViewById(R.id.brand_image);
		TextView tv4 = (TextView) view.findViewById(R.id.life_4).findViewById(
				R.id.brand_item_name);
		tv4.setText(list.get(3).getBrandSimpleName());
		imageLoader.displayImage(list.get(3).getLogo(), im4, options);
		ImageView im5 = (ImageView) view.findViewById(R.id.life_5)
				.findViewById(R.id.brand_image);
		TextView tv5 = (TextView) view.findViewById(R.id.life_5).findViewById(
				R.id.brand_item_name);
		tv5.setText(list.get(4).getBrandSimpleName());
		imageLoader.displayImage(list.get(4).getLogo(), im5, options);
		ImageView im6 = (ImageView) view.findViewById(R.id.life_6)
				.findViewById(R.id.brand_image);
		TextView tv6 = (TextView) view.findViewById(R.id.life_6).findViewById(
				R.id.brand_item_name);
		tv6.setText(list.get(5).getBrandSimpleName());
		imageLoader.displayImage(list.get(5).getLogo(), im6, options);
		ImageView im7 = (ImageView) view.findViewById(R.id.life_7)
				.findViewById(R.id.brand_image);
		TextView tv7 = (TextView) view.findViewById(R.id.life_7).findViewById(
				R.id.brand_item_name);
		tv7.setText(list.get(6).getBrandSimpleName());
		imageLoader.displayImage(list.get(6).getLogo(), im7, options);
		ImageView im8 = (ImageView) view.findViewById(R.id.life_8)
				.findViewById(R.id.brand_image);
		TextView tv8 = (TextView) view.findViewById(R.id.life_8).findViewById(
				R.id.brand_item_name);
		tv8.setText(list.get(7).getBrandSimpleName());
		imageLoader.displayImage(list.get(7).getLogo(), im8, options);

		ImageView im9 = (ImageView) view.findViewById(R.id.life_9)
				.findViewById(R.id.brand_image);
		TextView tv9 = (TextView) view.findViewById(R.id.life_9).findViewById(
				R.id.brand_item_name);
		tv9.setText(list.get(8).getBrandSimpleName());
		imageLoader.displayImage(list.get(8).getLogo(), im9, options);

		ImageView im10 = (ImageView) view.findViewById(R.id.life_10)
				.findViewById(R.id.brand_image);
		TextView tv10 = (TextView) view.findViewById(R.id.life_10)
				.findViewById(R.id.brand_item_name);
		tv10.setText(list.get(9).getBrandSimpleName());
		imageLoader.displayImage(list.get(9).getLogo(), im10, options);

		ImageView im11 = (ImageView) view.findViewById(R.id.life_11)
				.findViewById(R.id.brand_image);
		TextView tv11 = (TextView) view.findViewById(R.id.life_11)
				.findViewById(R.id.brand_item_name);
		tv11.setText(list.get(10).getBrandSimpleName());
		imageLoader.displayImage(list.get(10).getLogo(), im11, options);

		ImageView im12 = (ImageView) view.findViewById(R.id.life_12)
				.findViewById(R.id.brand_image);
		TextView tv12 = (TextView) view.findViewById(R.id.life_12)
				.findViewById(R.id.brand_item_name);
		tv12.setText(list.get(11).getBrandSimpleName());
		imageLoader.displayImage(list.get(11).getLogo(), im12, options);

		setImageClick(im1, list, 0);
		setImageClick(im2, list, 1);
		setImageClick(im3, list, 2);
		setImageClick(im4, list, 3);
		setImageClick(im5, list, 4);
		setImageClick(im6, list, 5);
		setImageClick(im7, list, 6);
		setImageClick(im8, list, 7);
		setImageClick(im9, list, 8);
		setImageClick(im10, list, 9);
		setImageClick(im11, list, 10);
		setImageClick(im12, list, 11);

	}

	protected void setEntertainmentValue(List<BrandDetail> list) {
		ImageView im1 = (ImageView) view.findViewById(R.id.entertainment_1)
				.findViewById(R.id.brand_image);
		TextView tv1 = (TextView) view.findViewById(R.id.entertainment_1)
				.findViewById(R.id.brand_item_name);
		tv1.setText(list.get(0).getBrandSimpleName());
		imageLoader.displayImage(list.get(0).getLogo(), im1, options);
		ImageView im2 = (ImageView) view.findViewById(R.id.entertainment_2)
				.findViewById(R.id.brand_image);
		TextView tv2 = (TextView) view.findViewById(R.id.entertainment_2)
				.findViewById(R.id.brand_item_name);
		tv2.setText(list.get(1).getBrandSimpleName());
		imageLoader.displayImage(list.get(1).getLogo(), im2, options);
		ImageView im3 = (ImageView) view.findViewById(R.id.entertainment_3)
				.findViewById(R.id.brand_image);
		TextView tv3 = (TextView) view.findViewById(R.id.entertainment_3)
				.findViewById(R.id.brand_item_name);
		tv3.setText(list.get(2).getBrandSimpleName());
		imageLoader.displayImage(list.get(2).getLogo(), im3, options);
		ImageView im4 = (ImageView) view.findViewById(R.id.entertainment_4)
				.findViewById(R.id.brand_image);
		TextView tv4 = (TextView) view.findViewById(R.id.entertainment_4)
				.findViewById(R.id.brand_item_name);
		tv4.setText(list.get(3).getBrandSimpleName());
		imageLoader.displayImage(list.get(3).getLogo(), im4, options);
		ImageView im5 = (ImageView) view.findViewById(R.id.entertainment_5)
				.findViewById(R.id.brand_image);
		TextView tv5 = (TextView) view.findViewById(R.id.entertainment_5)
				.findViewById(R.id.brand_item_name);
		tv5.setText(list.get(4).getBrandSimpleName());
		imageLoader.displayImage(list.get(4).getLogo(), im5, options);
		ImageView im6 = (ImageView) view.findViewById(R.id.entertainment_6)
				.findViewById(R.id.brand_image);
		TextView tv6 = (TextView) view.findViewById(R.id.entertainment_6)
				.findViewById(R.id.brand_item_name);
		tv6.setText(list.get(5).getBrandSimpleName());
		imageLoader.displayImage(list.get(5).getLogo(), im6, options);
		ImageView im7 = (ImageView) view.findViewById(R.id.entertainment_7)
				.findViewById(R.id.brand_image);
		TextView tv7 = (TextView) view.findViewById(R.id.entertainment_7)
				.findViewById(R.id.brand_item_name);
		tv7.setText(list.get(6).getBrandSimpleName());
		imageLoader.displayImage(list.get(6).getLogo(), im7, options);
		ImageView im8 = (ImageView) view.findViewById(R.id.entertainment_8)
				.findViewById(R.id.brand_image);
		TextView tv8 = (TextView) view.findViewById(R.id.entertainment_8)
				.findViewById(R.id.brand_item_name);
		tv8.setText(list.get(7).getBrandSimpleName());
		imageLoader.displayImage(list.get(7).getLogo(), im8, options);

		ImageView im9 = (ImageView) view.findViewById(R.id.entertainment_9)
				.findViewById(R.id.brand_image);
		TextView tv9 = (TextView) view.findViewById(R.id.entertainment_9)
				.findViewById(R.id.brand_item_name);
		tv9.setText(list.get(4).getBrandSimpleName());
		imageLoader.displayImage(list.get(4).getLogo(), im9, options);
		ImageView im10 = (ImageView) view.findViewById(R.id.entertainment_10)
				.findViewById(R.id.brand_image);
		TextView tv10 = (TextView) view.findViewById(R.id.entertainment_10)
				.findViewById(R.id.brand_item_name);
		tv10.setText(list.get(9).getBrandSimpleName());
		imageLoader.displayImage(list.get(9).getLogo(), im10, options);
		ImageView im11 = (ImageView) view.findViewById(R.id.entertainment_11)
				.findViewById(R.id.brand_image);
		TextView tv11 = (TextView) view.findViewById(R.id.entertainment_11)
				.findViewById(R.id.brand_item_name);
		tv11.setText(list.get(10).getBrandSimpleName());
		imageLoader.displayImage(list.get(10).getLogo(), im11, options);
		ImageView im12 = (ImageView) view.findViewById(R.id.entertainment_12)
				.findViewById(R.id.brand_image);
		TextView tv12 = (TextView) view.findViewById(R.id.entertainment_12)
				.findViewById(R.id.brand_item_name);
		tv12.setText(list.get(11).getBrandSimpleName());
		imageLoader.displayImage(list.get(11).getLogo(), im12, options);

		setImageClick(im1, list, 0);
		setImageClick(im2, list, 1);
		setImageClick(im3, list, 2);
		setImageClick(im4, list, 3);
		setImageClick(im5, list, 4);
		setImageClick(im6, list, 5);
		setImageClick(im7, list, 6);
		setImageClick(im8, list, 7);
		setImageClick(im9, list, 8);
		setImageClick(im10, list, 9);
		setImageClick(im11, list, 10);
		setImageClick(im12, list, 11);

	}

	protected void setFoodValue(List<BrandDetail> list) {
		ImageView im1 = (ImageView) view.findViewById(R.id.food_1)
				.findViewById(R.id.brand_image);
		TextView tv1 = (TextView) view.findViewById(R.id.food_1).findViewById(
				R.id.brand_item_name);
		// Log.i("品牌名称", list.get(0).getBrandSimpleName());
		if (list.get(0).getBrandSimpleName() != null
				&& !("").equals(list.get(0).getBrandSimpleName())) {
			tv1.setText(list.get(0).getBrandSimpleName());
		} else {
			tv1.setText("");
		}
		imageLoader.displayImage(list.get(0).getLogo(), im1, options);
		ImageView im2 = (ImageView) view.findViewById(R.id.food_2)
				.findViewById(R.id.brand_image);
		TextView tv2 = (TextView) view.findViewById(R.id.food_2).findViewById(
				R.id.brand_item_name);
		// Log.i("品牌名称", list.get(1).getBrandSimpleName());
		if (list.get(1).getBrandSimpleName() != null
				&& !("").equals(list.get(1).getBrandSimpleName())) {
			tv2.setText(list.get(1).getBrandSimpleName());
		} else {
			tv2.setText("");
		}
		imageLoader.displayImage(list.get(1).getLogo(), im2, options);
		ImageView im3 = (ImageView) view.findViewById(R.id.food_3)
				.findViewById(R.id.brand_image);
		TextView tv3 = (TextView) view.findViewById(R.id.food_3).findViewById(
				R.id.brand_item_name);
		// tv3.setText(list.get(2).getBrandSimpleName());
		// Log.i("品牌名称", list.get(2).getBrandSimpleName());
		if (list.get(2).getBrandSimpleName() != null
				&& !("").equals(list.get(2).getBrandSimpleName())) {
			tv3.setText(list.get(2).getBrandSimpleName());
		} else {
			tv3.setText("");
		}
		imageLoader.displayImage(list.get(2).getLogo(), im3, options);
		ImageView im4 = (ImageView) view.findViewById(R.id.food_4)
				.findViewById(R.id.brand_image);
		TextView tv4 = (TextView) view.findViewById(R.id.food_4).findViewById(
				R.id.brand_item_name);

		// tv4.setText(list.get(3).getBrandSimpleName());
		// Log.i("品牌名称", list.get(3).getBrandSimpleName());
		if (list.get(3).getBrandSimpleName() != null
				&& !("").equals(list.get(3).getBrandSimpleName())) {
			tv4.setText(list.get(3).getBrandSimpleName());
		} else {
			tv4.setText("");
		}
		imageLoader.displayImage(list.get(3).getLogo(), im4, options);

		ImageView im5 = (ImageView) view.findViewById(R.id.food_5)
				.findViewById(R.id.brand_image);
		TextView tv5 = (TextView) view.findViewById(R.id.food_5).findViewById(
				R.id.brand_item_name);
		if (list.get(4).getBrandSimpleName() != null
				&& !("").equals(list.get(4).getBrandSimpleName())) {
			tv5.setText(list.get(4).getBrandSimpleName());
		} else {
			tv5.setText("");
		}
		imageLoader.displayImage(list.get(4).getLogo(), im5, options);
		ImageView im6 = (ImageView) view.findViewById(R.id.food_6)
				.findViewById(R.id.brand_image);
		TextView tv6 = (TextView) view.findViewById(R.id.food_6).findViewById(
				R.id.brand_item_name);

		if (list.get(5).getBrandSimpleName() != null
				&& !("").equals(list.get(5).getBrandSimpleName())) {
			tv6.setText(list.get(5).getBrandSimpleName());
		} else {
			tv6.setText("");
		}
		imageLoader.displayImage(list.get(5).getLogo(), im6, options);
		ImageView im7 = (ImageView) view.findViewById(R.id.food_7)
				.findViewById(R.id.brand_image);
		TextView tv7 = (TextView) view.findViewById(R.id.food_7).findViewById(
				R.id.brand_item_name);
		if (list.get(6).getBrandSimpleName() != null
				&& !("").equals(list.get(6).getBrandSimpleName())) {
			tv7.setText(list.get(6).getBrandSimpleName());
		} else {
			tv7.setText("");
		}
		imageLoader.displayImage(list.get(6).getLogo(), im7, options);
		ImageView im8 = (ImageView) view.findViewById(R.id.food_8)
				.findViewById(R.id.brand_image);
		TextView tv8 = (TextView) view.findViewById(R.id.food_8).findViewById(
				R.id.brand_item_name);

		if (list.get(7).getBrandSimpleName() != null
				&& !("").equals(list.get(7).getBrandSimpleName())) {
			tv8.setText(list.get(7).getBrandSimpleName());
		} else {
			tv8.setText("");
		}
		imageLoader.displayImage(list.get(7).getLogo(), im8, options);

		ImageView im9 = (ImageView) view.findViewById(R.id.food_9)
				.findViewById(R.id.brand_image);
		TextView tv9 = (TextView) view.findViewById(R.id.food_9).findViewById(
				R.id.brand_item_name);
		if (list.get(8).getBrandSimpleName() != null
				&& !("").equals(list.get(8).getBrandSimpleName())) {
			tv9.setText(list.get(8).getBrandSimpleName());
		} else {
			tv9.setText("");
		}
		imageLoader.displayImage(list.get(8).getLogo(), im9, options);

		ImageView im10 = (ImageView) view.findViewById(R.id.food_10)
				.findViewById(R.id.brand_image);
		TextView tv10 = (TextView) view.findViewById(R.id.food_10)
				.findViewById(R.id.brand_item_name);

		if (list.get(9).getBrandSimpleName() != null
				&& !("").equals(list.get(9).getBrandSimpleName())) {
			tv10.setText(list.get(9).getBrandSimpleName());
		} else {
			tv10.setText("");
		}
		imageLoader.displayImage(list.get(9).getLogo(), im10, options);

		ImageView im11 = (ImageView) view.findViewById(R.id.food_11)
				.findViewById(R.id.brand_image);
		TextView tv11 = (TextView) view.findViewById(R.id.food_11)
				.findViewById(R.id.brand_item_name);
		if (list.get(10).getBrandSimpleName() != null
				&& !("").equals(list.get(10).getBrandSimpleName())) {
			tv11.setText(list.get(10).getBrandSimpleName());
		} else {
			tv11.setText("");
		}
		imageLoader.displayImage(list.get(10).getLogo(), im11, options);

		ImageView im12 = (ImageView) view.findViewById(R.id.food_12)
				.findViewById(R.id.brand_image);
		TextView tv12 = (TextView) view.findViewById(R.id.food_12)
				.findViewById(R.id.brand_item_name);

		if (list.get(11).getBrandSimpleName() != null
				&& !("").equals(list.get(11).getBrandSimpleName())) {
			tv12.setText(list.get(11).getBrandSimpleName());
		} else {
			tv12.setText("");
		}
		imageLoader.displayImage(list.get(11).getLogo(), im12, options);

		setImageClick(im1, list, 0);
		setImageClick(im2, list, 1);
		setImageClick(im3, list, 2);
		setImageClick(im4, list, 3);
		setImageClick(im5, list, 4);
		setImageClick(im6, list, 5);
		setImageClick(im7, list, 6);
		setImageClick(im8, list, 7);

		setImageClick(im9, list, 8);
		setImageClick(im10, list, 9);
		setImageClick(im11, list, 10);
		setImageClick(im12, list, 11);

	}

	private void setImageClick(ImageView im, final List<BrandDetail> list,
			final int position) {
		im.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				FragmentTransaction ft = getFragmentManager()
						.beginTransaction();
				BrandInfoFragment brandShopFragment = new BrandInfoFragment();
				Bundle b = new Bundle();
				b.putString("brandID", String.valueOf(list.get(position).getBrandID()));
				brandShopFragment.setArguments(b);
				ft.replace(R.id.fragment, brandShopFragment);
				ft.addToBackStack(null);
				ft.commit();

			}
		});
	}

}
